<?

class Mdl_Sites extends MY_Model {

    function Mdl_Sites() {

        parent::MY_Model();

        $this->limit = 25;

    }

    function get($params = NULL) {

        $this->prep_params($params);

        if (isset($params['site_id'])) {

            $this->db->where('site_id', $params['site_id']);

        }

        if (isset($params['url_title'])) {

            $this->db->where('url_title', $params['url_title']);

        }

        if (isset($params['is_default'])) {

            $this->db->where('is_default', 1);

        }

        if (isset($params['paginate']) AND $params['paginate'] == TRUE) {

            $offset = ($this->page * $this->limit) - $this->limit;

            $this->db->limit($this->limit, $offset);

        }

        if ($this->is_admin AND $this->session->userdata('user_type_id') <> 1 AND $this->session->userdata('user_type_id') <> 3) {

            $this->db->where('site_id', $this->session->userdata('site_id'));

        }

        $query = $this->db->get('inktype_sites');

        if (isset($params['paginate']) AND $params['paginate'] == TRUE) {

            $this->set_page_links();

        }

        if (isset($params['site_id']) OR isset($params['url_title']) OR isset($params['is_default'])) {

            return $query->row();

        }

        else {

            return $query->result();

        }

    }

    function save() {

        $db_array = $this->prep_db_array();

        $db_array['url_title'] = url_title($this->input->post('site_name'));

        if (!isset($this->uri_assoc['site_id'])) {

            $this->db->insert('inktype_sites', $db_array);

        }

        else {

            $this->db->where('site_id', $this->uri_assoc['site_id']);

            $this->db->update('inktype_sites', $db_array);

        }

        $this->session->set_flashdata('success_save', TRUE);

    }

    function delete() {

        if (isset($this->uri_assoc['site_id']) AND $this->session->userdata('user_type_id') == 1) {

            $this->db->where('site_id', $this->uri_assoc['site_id']);

            $this->db->delete('inktype_sites');

            $this->inktype_system->clean_orphans();

            $this->session->set_flashdata('success_delete', TRUE);

        }

    }

    function set_default($site_id) {

        $db_array = array(
            'is_default'    =>  0
        );

        $this->db->update('inktype_sites', $db_array);

        $db_array = array(
            'is_default'    =>  1
        );

        $this->db->where('site_id', $site_id);

        $this->db->update('inktype_sites', $db_array);

    }

    function set_site_session($site_id) {

        $session_data = array(
            'site_id'           =>  $site_id,
            'current_site_url'  =>  $this->inktype_system->current_site_url($site_id)
        );

        $this->session->set_userdata($session_data);

    }

    function validate() {

        $this->fields = array(
            'site_name'                 =>  'Site Name',
            'site_description'          =>  'Site Description',
            'template_folder'           =>  'Template Folder',
            'template_default_view'     =>  'Default View'
        );

        $this->rules = array(
            'site_name'                 =>  'required',
            'template_folder'           =>  'required',
            'template_default_view'     =>  'required'
        );

        return $this->_validate();

    }

}

?>